﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Devices;
using BCL.Tool.Extend;
namespace DevicesWPF
{
    /// <summary>
    /// UCDevicesState.xaml 的交互逻辑
    /// </summary>
    public partial class UCDevicesState : UserControl
    {
        IDevices dev;
        public UCDevicesState()
        {
            InitializeComponent();
        }
        public IDevices Dev { get { return dev; } }
        public UCDevicesState(IDevices _dev)
        {
            dev = _dev;
            InitializeComponent();
            string imagepath = dev.Info.GetImageFillPath();
            if (File.Exists(imagepath))
                this.pictureBox1.Source = File.ReadAllBytes(imagepath).ToBitmap();
            this.labelName.Text = dev.Info.DisplayName;
            dev.StateChanged += dev_StateChanged;
            dev_StateChanged(dev, dev.State);
        }

        void dev_StateChanged(IDevices obj, DevicesState e)
        {
            switch (e)
            {
                case DevicesState.Opened:
                    this.borderMain.BorderBrush = Brushes.Green;
                    break;
                case DevicesState.Error:
                    this.borderMain.BorderBrush = Brushes.Yellow;
                    break;
                case DevicesState.Closed:
                    this.borderMain.BorderBrush = Brushes.Gray;
                    break;
                case DevicesState.Unknown:
                    this.borderMain.BorderBrush = Brushes.Red;
                    break;
            }
        }
        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dev.ShowConfigForm();
        }

        private void 删除ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("是否确定要删除此项?", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                DevicesCollection.Remove(dev);
        }

        private void 查看队列ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            dev.ShowQueueForm();
        }

        private void 开启ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            dev.Open();
        }

        private void 关闭ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            dev.Close();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            UCDevicesConfig uCDevicesConfig = new UCDevicesConfig(dev);
            uCDevicesConfig.ShowDialog(null);
        }

        private void PictureBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
